
function GM:UpdatePlayerTime()
	if ( !self:IsBuild() or CLIENT or #player.GetAll() > 0 ) then
		self.PlayerTime = (self.PlayerTime or 0) + FrameTime()
	end
end
GM:AddHook( "Think", "UpdatePlayerTime" )

function GM:ToggleRound()
	self:SetIsBuild(!self:IsBuild()) -- Togglerific!

	if !self:IsBuild() and SERVER then
		self:SetRoundNumber(self:GetRoundNumber() + 1)
	end
	
	self:CallHook( "RoundChanged" )
	if ( SERVER ) then self:SetRoundLength(self:GetRoundTimer()) end
	
	-- The client needs to be notified of the changes.
	if ( SERVER ) then
		self:SendRound()
	end
end

function GM:SetIsBuild(build) SetGlobalBool("IsBuildRound", build) end
function GM:IsBuild() return GetGlobalBool("IsBuildRound", false) end
function GM:IsFight() return !self:IsBuild() end

function GM:GetNextRound()
	return GetGlobalFloat("NextRound", 0)
end
function GM:SetNextRound(time)
	SetGlobalFloat("NextRound", time)
end

-- Round timer fiddling functions.
function GM:SetRoundTimer( seconds )
	self:SetNextRound((self.PlayerTime or 0) + seconds)
	self:SendRoundTimer()
end
function GM:AddRoundTimer( seconds )
	self:SetNextRound(self:GetNextRound() + seconds)
	self:SendRoundTimer()
end
function GM:GetRoundTimer()
	return math.max( self:GetNextRound() - (self.PlayerTime or 0), 0 ) 
end

function GM:SetRoundLength( seconds )
	SetGlobalFloat("RoundLength", seconds or 0)
	self:SendRoundTimer()
end
function GM:GetRoundLength()
	return GetGlobalFloat("RoundLength", 0)
end

function GM:SetRoundNumber(round)
	SetGlobalInt("RoundNumber", round)
end
function GM:GetRoundNumber()
	return GetGlobalInt("RoundNumber", 0)
end

-- Sends the round to player(s).
function GM:SendRound( ply )
	if ( !SERVER ) then return end
	self:SendRoundTimer( ply )
	umsg.Start( "RoundChange", ply )
		umsg.Bool( self:IsBuild() ) -- I am fricking wacko today eh
		umsg.Short( self:GetRoundNumber() )
	umsg.End()
end
GM:AddHook( "PlayerInitialSpawn", "SendRound" )

function GM:ResendRound()
	for _, ply in pairs(player.GetAll()) do
		self:SendRound(ply)
	end
end
GM:AddHook( "OnReloaded", "ResendRound" )

-- Sends the round time to player(s).
function GM:SendRoundTimer( ply )
	if ( !SERVER ) then return end
	umsg.Start( "RoundTimer", ply ) -- If ply is nil, it gets sent to everyone.
		umsg.Short( self:GetRoundTimer() )
		umsg.Short( self:GetRoundLength() )
	umsg.End()
end


if ( SERVER ) then -- SERVER only.

-- Checks the time, and if it's gone, toggles the round.
function GM:CheckRoundTimer()
	if ( (self.PlayerTime or 0) > self:GetNextRound() ) then
		self:SetRoundTimer( 1 )
		self:ToggleRound()
	end
end
GM:AddHook( "Think", "CheckRoundTimer" )

function GM:RoundInName()
	local round = "Build"
	if ( self:IsFight() ) then round = "Fight" end
	return self.Name .. " [" .. round .. "]"
end
GM:AddHook( "GetGamemodeDescription", "RoundInName" )

end

-- Usermessage receiving.
if ( CLIENT ) then -- CLIENT only.

function GM.ReceiveRound( msg )
	local round = msg:ReadBool()
	GAMEMODE:SetRoundNumber(msg:ReadShort())
	if ( round != GAMEMODE:IsBuild() ) then
		GAMEMODE:ToggleRound()
	end
end
usermessage.Hook( "RoundChange", GM.ReceiveRound )
function GM.ReceiveRoundTimer( msg )
	local timer = msg:ReadShort()
	local length = msg:ReadShort()
	GAMEMODE:SetRoundTimer( timer )
	GAMEMODE:SetRoundLength( length )
end
usermessage.Hook( "RoundTimer", GM.ReceiveRoundTimer )

end -- End of CLIENT only.